package com.ruoyi.web.controller.coursewarefile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.YcCoursewareFile;
import com.ruoyi.system.service.IYcCoursewareFileService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 上传课件Controller
 * 
 * @author 杨琛
 * @date 2020-11-23
 */
@Controller
@RequestMapping("/system/file")
public class YcCoursewareFileController extends BaseController
{
    private String prefix = "system/file";

    @Autowired
    private IYcCoursewareFileService ycCoursewareFileService;

    @RequiresPermissions("system:file:view")
    @GetMapping()
    public String file()
    {
        return prefix + "/file";
    }

    /**
     * 查询上传课件列表
     */
    @RequiresPermissions("system:file:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YcCoursewareFile ycCoursewareFile)
    {
        startPage();
        List<YcCoursewareFile> list = ycCoursewareFileService.selectYcCoursewareFileList(ycCoursewareFile);
        return getDataTable(list);
    }

    /**
     * 导出上传课件列表
     */
    @RequiresPermissions("system:file:export")
    @Log(title = "上传课件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YcCoursewareFile ycCoursewareFile)
    {
        List<YcCoursewareFile> list = ycCoursewareFileService.selectYcCoursewareFileList(ycCoursewareFile);
        ExcelUtil<YcCoursewareFile> util = new ExcelUtil<YcCoursewareFile>(YcCoursewareFile.class);
        return util.exportExcel(list, "file");
    }

    /**
     * 新增上传课件
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存上传课件
     */
    @RequiresPermissions("system:file:add")
    @Log(title = "上传课件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YcCoursewareFile ycCoursewareFile)
    {
        return toAjax(ycCoursewareFileService.insertYcCoursewareFile(ycCoursewareFile));
    }

    /**
     * 修改上传课件
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        YcCoursewareFile ycCoursewareFile = ycCoursewareFileService.selectYcCoursewareFileById(id);
        mmap.put("ycCoursewareFile", ycCoursewareFile);
        return prefix + "/edit";
    }

    /**
     * 修改保存上传课件
     */
    @RequiresPermissions("system:file:edit")
    @Log(title = "上传课件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YcCoursewareFile ycCoursewareFile)
    {
        return toAjax(ycCoursewareFileService.updateYcCoursewareFile(ycCoursewareFile));
    }

    /**
     * 删除上传课件
     */
    @RequiresPermissions("system:file:remove")
    @Log(title = "上传课件", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ycCoursewareFileService.deleteYcCoursewareFileByIds(ids));
    }
}
